﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Jy123.DBUtility;

namespace Jy123.Common
{
    public class Cookie
    {
        /// <summary>
        /// 创建Cookies
        /// </summary>
        /// <param name="key">名称</param>
        /// <param name="value">内容</param>
        /// <param name="setDay">过期天数</param>
        public static bool SetCookie(string key, string value, int setDay)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(key);
                Cookie.Expires = DateTime.Now.AddDays(setDay);
                Cookie.Value = value;
                //Cookie.Domain = PubConstant.GetConfigString("Domain");
                //Cookie.Path = "/"; 
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 创建Cookies
        /// </summary>
        /// <param name="key">名称</param>
        /// <param name="value">内容</param>
        /// <param name="setDay">过期分钟</param>
        public static bool SetCookieM(string key, string value, int setMinutes)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(key);
                Cookie.Expires = DateTime.Now.AddMinutes(setMinutes);
                Cookie.Value = value;
                //Cookie.Domain = PubConstant.GetConfigString("Domain");
                //Cookie.Path = "/"; 
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 创建Cookie
        /// </summary>
        /// <param name="key">名称</param>
        /// <param name="value">内容</param>
        /// <returns></returns>
        public static bool SetCookie(string key, string value)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(key);
                Cookie.Value = value;
                //Cookie.Domain = PubConstant.GetConfigString("Domain");
                //Cookie.Path = "/"; 
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="key">名称</param>
        /// <returns>内容</returns>
        public static string getCookie(string key)
        {
            HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[key];
            if (Cookie != null)
            {
                return Cookie.Value.ToString();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除Cookie
        /// </summary>
        /// <param name="key">名称</param>
        /// <returns>内容</returns>
        public static bool DelCookie(string key)
        {
            if (System.Web.HttpContext.Current.Request.Cookies[key] != null)
            {
                HttpCookie Cookie = new HttpCookie(key);
                //Cookie.Domain = PubConstant.GetConfigString("Domain");
                //Cookie.Path = "/";
                Cookie.Expires = DateTime.Now.AddDays(-1);
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
